package main.java.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import main.java.DAO.IEnrollDAO;
import main.java.constant.StateEnum;
import main.java.model.EnrollMetadataModel;
import main.java.service.EnrollService;
import main.java.service.JsonService;
import main.java.service.ResourceService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


@Controller
@RequestMapping("/enroll")
class EnrollController {
	@Autowired
	@Qualifier("enrollService")
	private EnrollService enrollService;
	@Autowired
	@Qualifier("resourceService")
	ResourceService resourceService;
	@Autowired
	@Qualifier("jsonService")
	JsonService jsonService;
	@Autowired
	@Qualifier("IEnrollDAO")
	private IEnrollDAO enrollDAO;

	/* create */
	@RequestMapping(method = RequestMethod.POST, value = "/create/")
	public String create(ModelMap modelMap, 
									@RequestBody String json
	) throws UnsupportedEncodingException	 {
		String ripeJson = jsonService.restoreJsonFromString(json);
		
		StateEnum state = StateEnum.OTHER;
		state = enrollService.create(ripeJson);
		modelMap.put("state", state);
		
		return "main/resources/JSP/state";
	}
	
	/* createImg */
	@RequestMapping(method = RequestMethod.POST, value = "/createImg/")
	public String createImg(ModelMap modelMap,  
											UploadItem uploadItem,
											HttpServletRequest request
				) throws IOException			{
		String imgPath = enrollService.createImg(uploadItem.getEnrollImg(), request.getServletPath());
		String imgUrl = resourceService.transFileAndUrlPath(imgPath);
		modelMap.put("state", imgUrl);

		return "main/resources/JSP/state";
	}
	
	/* getAll */
	@RequestMapping(method = RequestMethod.GET, value = "/getAll/")
	public String getAll(ModelMap modelMap,  
									HttpServletRequest request
	) 		{
		Gson gson = new Gson();
		List<EnrollMetadataModel> allMetadata = enrollDAO.getAllMetadata();
		String json = gson.toJson(allMetadata);
		modelMap.put("state", json);
		
		return "main/resources/JSP/state";
	}
	
	/* getActive */
	@RequestMapping(method = RequestMethod.GET, value = "/getActive/")
	public String getActive(ModelMap modelMap,  
									HttpServletRequest request
	) 		{
		Gson gson = new Gson();
		List<EnrollMetadataModel> allActiveMetadata = enrollDAO.getAllActiveMetadata();
		String json = gson.toJson(allActiveMetadata);
		modelMap.put("state", json);
		
		return "main/resources/JSP/state";
	}
	
	/* submit */
	@RequestMapping(method = RequestMethod.POST, value = "/submit/")
	public String submit(ModelMap modelMap, 
									@RequestBody String json
	) throws UnsupportedEncodingException	 {
		String ripeJson = jsonService.restoreJsonFromString(json);
		
		StateEnum state = StateEnum.OTHER;
		state = enrollService.submit(ripeJson);
		modelMap.put("state", state);
		
		return "main/resources/JSP/state";
	}
	
	public static class UploadItem		{
		CommonsMultipartFile enrollImg;

		public UploadItem() {
		}
		
		public CommonsMultipartFile getEnrollImg() {
			return enrollImg;
		}

		public void setEnrollImg(CommonsMultipartFile enrollImg) {
			this.enrollImg = enrollImg;
		}
		
	}
}
